package com.zjy.software.day16;

import java.io.*;

public class ObjectStreamDemo {
    public static void main(String[] args) throws Exception
    {
//       testObjectWriter();
        testObjectReader();
    }

    private static void testObjectReader() {
        try {
            ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream(("object.txt")));
            Object o = objectInputStream.readObject();
            Person person=(Person)o;
            System.out.println("person = " + person);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void testObjectWriter() throws IOException {
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream("object.txt"));
        objectOutputStream.writeObject(new Person("zhangsan",11,"beijing"));
        objectOutputStream.close();
    }
}
class Person implements java.io.Serializable{
    public Person(String name) {
        this.name = name;
    }

    public Person(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }

    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    private transient int age;
    private static String address;


}